using System;

namespace TodoSoft.Data.Migration
{
	internal class SqlCeVersionRepo : IVersionRepo
	{
		public int GetCurrentVersion(ISqlCommandProcessor sql, Guid componentId)
		{
			if (!sql.IsTableExist (VersionMigration.VersionTableName))
			{
				return -1;
			}
			
			var result = sql.ExecuteScalar ("select max(versionnumber) from version where componentid = ?", componentId);
			
			if (result == DBNull.Value)
				return -1;

			return (int)result;
		}

		public void UpdateVersion(ISqlCommandProcessor sql, Guid componentId, string componentName, int versionNumber, string versionDesc)
		{
			sql.ExecuteNonQuery (
				@"insert into version (versionid, componentid, versionnumber, versionname, componentname)values(@p0, @p1, @p2, @p3, @p4)",
				Guid.NewGuid (),
				componentId,
				versionNumber,
				versionDesc,
				componentName);
		}
	}
}

